using MediatR;
using STD.Shared.Common.Helpers;
using STD.Services.UserService.Domain.Entities;
using STD.Services.UserService.Domain.Repositories;

namespace STD.Services.UserService.Application.Queries;

/// <summary>
/// 获取用户查询
/// </summary>
public class GetUserQuery : IRequest<Result<User>>
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public Guid Id { get; set; }
}

/// <summary>
/// 获取用户查询处理器
/// </summary>
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, Result<User>>
{
    private readonly IUserRepository _userRepository;
    
    public GetUserQueryHandler(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }
    
    public async Task<Result<User>> Handle(GetUserQuery request, CancellationToken cancellationToken)
    {
        var user = await _userRepository.GetByIdAsync(request.Id);
        
        if (user == null)
        {
            return Result<User>.Failure("用户不存在");
        }
        
        return Result<User>.Success(user);
    }
}

/// <summary>
/// 获取所有用户查询
/// </summary>
public class GetAllUsersQuery : IRequest<Result<IEnumerable<User>>>
{
    /// <summary>
    /// 跳过数量
    /// </summary>
    public int Skip { get; set; } = 0;
    
    /// <summary>
    /// 获取数量
    /// </summary>
    public int Take { get; set; } = 100;
}

/// <summary>
/// 获取所有用户查询处理器
/// </summary>
public class GetAllUsersQueryHandler : IRequestHandler<GetAllUsersQuery, Result<IEnumerable<User>>>
{
    private readonly IUserRepository _userRepository;
    
    public GetAllUsersQueryHandler(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }
    
    public async Task<Result<IEnumerable<User>>> Handle(GetAllUsersQuery request, CancellationToken cancellationToken)
    {
        var users = await _userRepository.GetAllAsync(request.Skip, request.Take);
        return Result<IEnumerable<User>>.Success(users);
    }
}

public interface IQuery<T> : IRequest<T>
{
}

public interface IQueryHandler<TQuery, TResponse> : IRequestHandler<TQuery, TResponse> where TQuery : IQuery<TResponse>
{
} 